home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.ucsb.edu!usenet
- From: "Peter V. Kharchenko" <peter@cs.ucsb.edu>
- Newsgroups: comp.lang.c++
- Subject: Basic header file broblem in BC 4.5
- Date: 21 Jan 1996 20:48:00 GMT
- Organization: UCSB
- Message-ID: <4du8q0$k1h@yuggoth.ucsb.edu>
- NNTP-Posting-Host: engrhub.ucsb.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; OSF1 V2.0 alpha)
- X-URL: news:comp.lang.c++
-
- Hi everyone,
-
- I've been trying for a several days to correct my project file in some way to
- make it work. Here's the situation that I have:
-
- I have two header files (.hpp) with little (.cpp) files corresponding to them,
- and one file that includes main() function in it. I am using Borland C 4.5. I
- created a project and included there two .cpp files corresponding to headers
- and the main file. The problem is that Linker does not recognize the functions
- described in one of the .cpp (.hpp) files. The whole thing works perfectly if
- I just attach function bodies (ones that are normally in little .cpp file) to
- the .hpp file. Anyway .. I'll enclose the files later on.
-
- Actual problem: I get a linker error: undifined object in task01.cpp :
- stack<complex>::<complex> pop()
- etc. on all the other stack functions. (notice that all the functions
- described in complex.cpp are workign just fine, it's just the stack unit that
- is screwed)
-
- here's how my project window looks like:
- o task01.exe
- o complex.cpp
- o stack.cpp
- o task01.cpp
-
- Sources for the files:
-
- // task01.cpp
- #include <iostream.h>
- #include "complex.h"
- #include "stack.h"
-
- istream& operator>>(istream& s, complex& a)
- {
- double re=0, im=0;
- char c=0;
-
- s >> c;
- if (c=='(') {
- s >> re >> c;
- if (c== ',') s >> im >> c;
- if (c!= ')') s.clear(ios::failbit);
- }
- else {
- s.putback(c);
-
- }
-
- if (s) a=complex(re,im);
- return s;
- }
-
- main() {
-
- complex temp;
- char c;
- Stack<complex> values(40);
- do
- {
- cin.get(c);
- if (c=='(') {
- cin.putback(c); cin >> temp;
- if (cin.fail()) cerr << "Corrupted data !!!\n"; else
- values.push
- (temp);
- }
- else if (c=='*') { values.push(values.pop()*values.pop()); }
- else if (c=='+') { values.push(values.pop()+values.pop()); }
- else if (c!='\n') cerr << "Corrupted data !!!\n";
- }
- while (c!='\n');
-
- if (!cin.fail()) cout << "Answer:" << values.pop() << endl;
- return(0);
- }
-
-
- --------------------------------------------------------------------------------
-
- //stack.hpp
- #ifndef STACK_H
- #define STACK_H 1
- template <class KeyType> class Stack {
- private:
- int top;
- KeyType *stack;
- int MaxSize;
-
- public:
- Stack(int MaxStackSize = 100);
- int IsFull();
- int IsEmpty();
- void push(const KeyType& item);
- KeyType pop();
- };
-
-
-
- --------------------------------------------------------------------------------
-
- //stack.cpp
- template <class KeyType> Stack<KeyType>::Stack(int
- MaxStackSize):MaxSize(MaxStackSize)
- {
- stack=new KeyType[MaxSize];
- top=-1;
- }
-
- template <class KeyType> inline int Stack<KeyType>::IsFull()
- {
- if (top == MaxSize-1) return TRUE;
- else return FALSE;
- }
-
- template <class KeyType> inline int Stack<KeyType>::IsEmpty()
- {
- if(top==-1) return TRUE;
- else return FALSE;
- }
-
- template <class KeyType> void Stack<KeyType>::push(const KeyType& x)
- {
- if(IsFull()) cerr << "Error: Stack is FULL !!!";
- else stack[++top]=x;
- }
-
- template <class KeyType> KeyType Stack<KeyType>::pop()
- {
- if(IsEmpty()) {cerr << "Error: Stack is EMPTY !!!"; return 0;}
- return stack[top--];
- }
- #endif
-
-
-
- -------------------------------------------------------------------------------
-
- // complex.hpp
- #ifndef COMPLEX_H
- #define COMPLEX_H 1
- #include <iostream.h>
- class complex {
- double re, im;
- public:
- complex(double,double);
- complex();
-
- friend complex operator+(const complex&,const complex&);
- friend complex operator-(const complex&,const complex&);
- friend complex operator*(const complex&,const complex&);
- friend ostream& operator<<(ostream&,const complex) ;
- void operator=(const complex& );
- };
-
-
- --------------------------------------------------------------------------------
-
- //complex.cpp
- complex::complex(double r, double i =0 ) { re=r; im=i; };
- complex::complex() { re=0; im=0; };
-
- complex operator+(const complex& lhs,const complex& rhs)
- { return complex(lhs.re+rhs.re,lhs.im+rhs.im); };
-
- complex operator-(const complex& lhs,const complex& rhs)
- { return complex(lhs.re-rhs.re,lhs.im-rhs.im); };
-
- complex operator*(const complex& lhs,const complex& rhs)
- { return
- complex(lhs.re*rhs.re-lhs.im*rhs.im,lhs.re*rhs.im+lhs.im*rhs.re);};
-
- void complex::operator=(const complex& rhs)
- { re=rhs.re; im=rhs.im; }
-
- ostream& operator<<(ostream& os, const complex val)
- {
- return os << '(' << val.re << ',' << val.im << ')';
- }
-
- #endif
-
-
-
-
- Thanx,
-
- Peter.
-
-